#include "widget.h"
#include "ui_widget.h"

#include <QtNetwork/QHostAddress>
#include <QTableWidgetItem>
#include <QMessageBox>
#include <QStringList>
#include <QtXml/QDomElement>
#include <QtXml/QDomDocument>
#include <QFile>

#include "global.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_bIsBoot = false;
    m_nCur = 0;
    m_nMax = 0;
    m_nMin = 0;
    m_nAvg = (m_nMax + m_nMin)/2;

    QString str;
    str.setNum(m_nCur);
    ui->m_lbCur->setText(str);
    str.clear();
    str.setNum(m_nMax);
    ui->m_lbMax->setText(str);
    str.clear();
    str.setNum(m_nMin);
    ui->m_lbMin->setText(str);
    str.clear();
    str.setNum(m_nAvg);
    ui->m_lbAvg->setText(str);


    //============get ip and port from confi.xml file=============
    QFile file("Configure\\confi.xml");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QString str1 = file.readAll();
    QDomDocument doc;
    doc.setContent(str1,false);
    QDomNodeList list = doc.elementsByTagName("server");

    QString ip = list.at(0).firstChildElement("ip").text();
    QString port = list.at(0).firstChildElement("port").text();
    ui->m_leIP->setText(ip);
    ui->m_lePort->setText(port);
    //=============================================================

    connect(&m_Server,SIGNAL(NewConnection(QString,QString,QString,int)),this,SLOT(processNewConnection(QString,QString,QString,int)));
    connect(&m_Server,SIGNAL(DisConnection(QString,QString,QString,int)),this,SLOT(processDisConnection(QString,QString,QString,int)));
    connect(this,SIGNAL(serverClose()),&m_Server,SIGNAL(serverClose()));

    ui->m_tbState->setText(QString(tr("<font color=red>Unboot</font>")));

    ///=================init the frend list==================================
    ui->m_twUserList->setColumnCount(4);
    ui->m_twUserList->setColumnWidth(0,110); //user id
    ui->m_twUserList->setColumnWidth(1,110); //nick name
    ui->m_twUserList->setColumnWidth(2,150); //ip
    ui->m_twUserList->setColumnWidth(3,80); //port


    QStringList HeadrList;
    HeadrList<<QObject::tr("User ID")<<QObject::tr("Nick Name")<<QObject::tr("IP")
        <<QObject::tr("Port");

    ui->m_twUserList->setHorizontalHeaderLabels(HeadrList);
    //=======================================================================
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_m_pbBoot_clicked()
{
    if (!m_bIsBoot)
    {
        QHostAddress ip(ui->m_leIP->text());
        int port = ui->m_lePort->text().toInt();

        if (!m_Server.isListening())
        {
           if (m_Server.listen(ip,port))
               ui->m_pbBoot->setText("Stop");
        }
        else
        {

        }
        m_bIsBoot = true;
        ui->m_tbState->setText(QString(tr("<font color=green>Running</font>")));
        ui->m_pbBoot->setText("Stop");
    }
    else
    {
        m_Server.close();
        m_bIsBoot = false;

        ui->m_tbState->setText(QString(tr("<font color=red>Unboot</font>")));
        ui->m_pbBoot->setText("Boot");

        emit serverClose();
    }
}
///add this new client to the table widget and the DOM
void Widget::processNewConnection(QString uid,QString nickname,QString ip,int port)
{
    m_nCur++;
    QString str;
    str.setNum(m_nCur);
    ui->m_lbCur->setText(str);

    int clientCount = ui->m_twUserList->rowCount();
    ui->m_twUserList->insertRow(clientCount);
    ui->m_twUserList->setItem(clientCount,0,new QTableWidgetItem(uid));
    ui->m_twUserList->setItem(clientCount,1,new QTableWidgetItem(nickname));
    ui->m_twUserList->setItem(clientCount,2,new QTableWidgetItem(ip));
    ui->m_twUserList->setItem(clientCount,3,new QTableWidgetItem(QString().setNum(port)));

}
///delete a client from the table widget and refresh
void Widget::processDisConnection(QString uid,QString nickname,QString ip,int port)
{
    if (m_nCur > 0)
    {
        m_nCur--;
        QString str;
        str.setNum(m_nCur);
        ui->m_lbCur->setText(str);

        int remove = 0;
        int count = ui->m_twUserList->rowCount();
        for (remove=0; remove<count; remove++)
            if (ui->m_twUserList->item(remove,3)->text()==QString().setNum(port))
                break;
        ui->m_twUserList->removeRow(remove);
    }
}
